import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import VuexStore from '@/store/index'
const OriginPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location, onResolve, onReject) {
  if (onResolve || onReject) return OriginPush.call(this, location, onResolve, onReject)
  return OriginPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',//首页
    name: 'Home',
    component: Home
  },
  {
    path: '/cart',//购物车
    name: 'Cart',
    meta: { needLogin: true },
    component: () => import('../views/Cart.vue')
  },
  {
    path: '/category',//分类
    name: 'Category',
    component: () => import('../views/Category.vue')
  },
  {
    path: '/user',//个人中心
    name: 'User',
    meta: { needLogin: true },
    component: () => import('../views/User.vue')
  },
  {
    path: '/user/data',//设置
    name: 'Data',
    component: () => import('../components/Data.vue')
  },
  {
    path: '/user/assets',//个人资产
    name: 'Assets',
    component: () => import('../components/Assets.vue')
  },
  {
    path: '/search',//搜索
    name: 'Search',
    component: () => import('../views/Search.vue')
  },
  // {
  //   path: '/service',//客服
  //   name: 'Service',
  //   meta:{needLogin:true},
  //   component: () => import( '../views/Service.vue')
  // },
  {
    path: '/login',//登录
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/order',//
    name: 'Order',
    component: () => import('../views/Order.vue'),
    caseSensitive: false,
    children: [
      {
        path: 'list/:listid',
        name: "List",
        props: true,
        component: () => import('@/views/List.vue')
      }
    ]
  },
  {
    path: '/productDetails/:detailID',//详情
    name: 'ProductDetails',
    meta: { Nav: true },
    props: true,
    component: () => import('../views/ProductDetails.vue')
  },
  {
    path: '/Submit',
    name: 'Submit',
    component: () => import('../views/Submit.vue')
  },
  {
    path: '/Pay',
    name: 'Pay',
    component: () => import('../views/Pay.vue')
  },
  {
    path: '/Fine',
    name: 'Fine',
    component: () => import('../views/Fine.vue')
  },
  {
    path: '/categorylist/:seachid',//分类里面的
    name: 'Categorylist',
    props: true,
    component: () => import('@/views/Categorylist.vue')
  }
]

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
});
//进来
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin && !VuexStore.getters.logintoken) {
    next('/login');
  }
  next();
})
//离开
// router.afterEach((to, from) => {
//   console.log(12313);
// });
//
// router.beforeResolve((to, from, next) => {
//   console.log("what");
//   next();
// });
export default router
